//
//  ProfitCaculator.js
//  SugLisSystem
//
//  Created by Mr.Right on 2013-04-07.
//  Copyright 2013 Mr.Right. All rights reserved.
//

var dB = require('untils/DB');
var exeSQL = require('untils/ExeSQL');

function ProfitCaculator() {

}

function calculate(lsPolSchema) {

	var headFlag = [];

	var calculateData = new Object();

	var riskCode = lsPolSchema['riskcode']
	var tableP_sql = "Select * From ProceedsExpress Where RiskCode='" + riskCode + "' order by CalOrderNo";
	var ProceedsExpressDB = new dB('ProceedsExpress');
	var ProceedsExpressSet = ProceedsExpressDB.executeQuery(tableP_sql);
	for (var i = 0; i < ProceedsExpressSet.length; i++) {
		var ProceedsExpressSchema = ProceedsExpressSet[i];
		var proceedsCode = ProceedsExpressSchema['proceedscode'];
		Ti.API.warn('[ProfitCaculator]执行算法代码:' + proceedsCode);
		headFlag.push(proceedsCode);
		//定义数据
		calculateData[proceedsCode] = [];
		var CalcuteElemetDB = new dB('CalcuteElemet');
		var tableC_sql = "Select * From CalcuteElemet Where RiskCode = '" + riskCode + "' And ItemCode = '" + proceedsCode + "' Order By CalOrderNo"
		var CalcuteElemetSet = CalcuteElemetDB.executeQuery(tableC_sql);
		for (var j = 0; j < CalcuteElemetSet.length; j++) {
			var CalcuteElemetSchema = CalcuteElemetSet[j];
			//sql计算方法
			var sqlExcuteType = CalcuteElemetSchema['sqlexcutetype'];
			// sql表达式
			var calSql = CalcuteElemetSchema['calsql'];
			//处理开始和结束点
			var startPoint = CalcuteElemetSchema['startpoint'];
			if (startPoint == null) {
				startPoint = '1';
			}
			//表达式计算
			startPoint = eval(transExpress(startPoint, lsPolSchema));
			var startPointFlag = CalcuteElemetSchema['startpointflag'];
			//处理开始标记
			if (startPointFlag == 'A') {
				startPoint = startPoint - inLSInsuredSchema['age'];
			}
			var endPoint = CalcuteElemetSchema['endpoint'];
			//表达式计算
			endPoint = eval(transExpress(endPoint, lsPolSchema));
			var endPointFlag = CalcuteElemetSchema['endpointflag'];
			//处理开始标记
			if (endPointFlag == 'A') {
				endPoint = endPoint - inLSInsuredSchema['age'];
			}

			//判断计算类型
			if (sqlExcuteType == 0) {
				var data = exeSQL.execSQL(transExpress(calSql, lsPolSchema), exeSQL.TYPE_ARRAY);
				for (var m = startPoint - 1; m < endPoint && m < data.length; m++) {
					calculateData[proceedsCode][m] = Math.round(data[m][0] * 100) / 100;
				}
			} else if (sqlExcuteType == 1) {
				var data = exeSQL.getOneValue(transExpress(calSql, lsPolSchema));
				for (var m = startPoint - 1; m < endPoint; m++) {
					calculateData[proceedsCode][m] = Math.round(data * 100) / 100;
				}
			}
		}
	}

	var profitData = {
		hData : headFlag,
		cData : calculateData

	}

	return profitData;
}

exports.calculate = calculate;
/**
 * 用LSPOL表中数据替换表达式
 * @param express 表达式
 * @param lsPolSchema POL表数据
 * */
function transExpress(express, lsPolSchema) {
	//修正大小写问题
	var expressArray = express.split('?');
	var tempExpress = '';
	for (var i = 0; i < expressArray.length; i++) {
		if (i % 2 != 0) {
			tempExpress += ('?' + expressArray[i].toLowerCase() + '?');
		} else {
			tempExpress += expressArray[i];
		}
	}
	// LSPOL表替换
	for (var obj in lsPolSchema) {
		eval("tempExpress = tempExpress.replace(/\\?" + obj + "\\?/g, lsPolSchema." + obj + ");");
	}
	// 被保人信息的特殊替换
	tempExpress = tempExpress.replace(/\?appage\?/g, lsPolSchema['insuredappage']);
	tempExpress = tempExpress.replace(/\?sex\?/g, lsPolSchema['insuredsex']);

	return tempExpress;
}

/**
 * 转换成HTML代码
 * @param data 数据
 * */
function toHTML(data) {
	var cData = data.cData;
	var hData = data.hData;
	var years = cData[hData[0]].length;

	var html = '';
	html += '<table>';
	for (var m = 0; m < years; m++) {
		if (m % 2 == 1) {
			html += '<tr align="right" valign="middle" bgcolor="#FFFFFF" class="code">';
		} else {
			html += '<tr align="right" valign="middle" bgcolor="#E9F3FE" class="black">';
		}
		for (var n = 0; n < hData.length; n++) {
			var tempData = cData[hData[n]][m];

			html += ('<td align="center" valign="middle" class="hei" nowrap="" style="">' + (tempData == 0 ? '--' : tempData) + '</td>');
		}
		html += '</tr>';
	}
	html += '</table>';
	return html;
}

exports.toHTML = toHTML;
